﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service.Repository.Sys;
using Domain.Entity;
using CommonCode;
using Service.App;

namespace WebApp.Controllers
{
    public class LoginController : Controller
    {
        private IUserRepository<Sys_User> user;
        public LoginController(IUserRepository<Sys_User> _user)
        {
            user = _user;
        }
        // GET: Login
        public ActionResult Index()
        {
            ViewBag.Title ="欢迎登录";
            return View();
        }

        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CheckLogin(string usercode, string password, string code)
        {
            try
            {
                if (Session["syc_session_verifycode"].IsEmpty() || Md5.md5(code.ToLower(), 16) != Session["syc_session_verifycode"].ToString())
                {
                    throw new Exception("验证码错误，请重新输入");
                }
                Sys_User model = new LoginApp(user).CheckLogin(usercode, password);
                if (model != null)
                {
                    OperatorModel operatorModel = new OperatorModel();
                    operatorModel.Id = model.Id;
                    operatorModel.UserCode = model.UserCode;
                    operatorModel.UserName = model.UserName;
                    operatorModel.UserNickName = model.UserNickName;
                    operatorModel.UserRole = model.UserRole;
                    operatorModel.IpAdress = Net.Parm_IpAdress;
                    operatorModel.IpAdressName = Net.GetLocation(operatorModel.IpAdress);
                    operatorModel.LoginTime = DateTime.Now;
                    operatorModel.LoginToken = Guid.NewGuid().ToString();
                    OperatorProvider.Provider.AddCurrent(operatorModel);
                    Logger.Default.Process(new Sys_Log("Info", "登录成功", "用户登录", "/LoginController/CheckLogin", usercode, "", true));
                }
                return Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功。" }.ToJson());
            }
            catch (Exception ex)
            {
                Logger.Default.Process(new Sys_Log("Info", "登录失败:" + ex.Message, "用户登录", "/LoginController/CheckLogin", usercode, "", false));
                return Content(new AjaxResult { state = ResultType.error.ToString(), message = ex.Message }.ToJson());
            }
        }

        [HttpGet]
        public ActionResult SignOut()
        {
            Logger.Default.Process(new Sys_Log("Info", "安全退出系统", "用户登录", "/LoginController/SignOut", OperatorProvider.Provider.GetCurrent().UserCode, "", true));
            Session.Abandon();
            Session.Clear();
            OperatorProvider.Provider.RemoveCurrent();
            return RedirectToAction("Index", "Login");
        }
    }
}